from flask import request
from flask_restful import Resource

from database import Database


class GetBooksByStackId(Resource):
    def post(self):
        data = request.get_json()
        stackId = data['stackId']
        print("stackId" + str(stackId))
        db = Database()
        query = "select * from book where stackId=%s and status='Available'"
        books = db.execute_query(query, (stackId,))
        if books:
            book_list = []
            print(books)
            for book in books:

                book_json = {
                    "bookId": book[0],
                    "categoryId": book[1],
                    "imgUrl": book[2],
                    "stackId": book[3],
                    "ownerId": book[4],
                    "condition": book[5],
                    "fee": str(book[6]),
                    "price": str(book[7]),
                    "status": book[8],
                }
                book_list.append(book_json)
                db.close_connection()
            return {"books": book_list}, 200
        else:
            db.close_connection()
            return {"message": "No books found for stackId"}, 404
